package Exo3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class Occurence {

	
	public static void main(String[] args) throws FileNotFoundException {
		HashMap<String,Integer> table=new HashMap<String,Integer>();
		TreeMap<String,Integer> arbre=new TreeMap<String,Integer>();
		Scanner input = new Scanner(new File("test.txt"));
		String line = null;
		while (input.hasNextLine()){
		    line = input.nextLine();
		    Scanner s = new Scanner(line);
		    s.useDelimiter("[\\\\$+:;,!?()\\-{}\\s\"'*/=\\[\\].<>|`~]+");
		    while (s.hasNext()){
		    	String mot = s.next().toUpperCase();
			  	if (table.containsKey(mot)){
		    		int c=table.get(mot);
		    		table.put(mot, (c+1));
		    	}else{
		    		table.put(mot, 1);
		    	}
			  	if (arbre.containsKey(mot)){
		    		int c=arbre.get(mot);
		    		arbre.put(mot, (c+1));
		    	}else{
		    		arbre.put(mot, 1);
		    	}
		    }
		}
		
		
		
		System.out.println("Affichage du HashMap");	
		Set<Map.Entry<String,Integer>> afftable=table.entrySet();
		Iterator<Map.Entry<String, Integer>> it=afftable.iterator();
		while (it.hasNext()) {
			Entry<String,Integer> paire=it.next();
			String nom=paire.getKey();
			Integer nb=paire.getValue();
			System.out.println(nom+" = "+nb);
		}
		
		System.out.println("\n \n Affichage du TreeMap");	
		Set<Map.Entry<String,Integer>> afftree=arbre.entrySet();
		Iterator<Map.Entry<String, Integer>> ita=afftree.iterator();
		while (ita.hasNext()) {
			Entry<String,Integer> pair=ita.next();
			String name=pair.getKey();
			Integer nbr=pair.getValue();
			System.out.println(name+" = "+nbr);
		}
		
	}

}
